package com.xiaodu.acer.phoneplay.data;

import android.content.Context;

import com.xiaodu.acer.phoneplay.common.Constant;
import com.xiaodu.acer.phoneplay.common.apkparset.AndroidApk;
import com.xiaodu.acer.phoneplay.common.util.AppUtils;
import com.xiaodu.acer.phoneplay.presenter.contract.AppManagerContract;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import library.util.ACache;
import zlc.season.rxdownload2.RxDownload;
import zlc.season.rxdownload2.entity.DownloadRecord;

/**
 */

public class AppManagerModel implements AppManagerContract.Model {


    private RxDownload mRxDownload;
    private Context mContext;

    public AppManagerModel(Context context, RxDownload rxDownload) {

        this.mContext = context;
        mRxDownload = rxDownload;
    }

    @Override
    public Observable<List<DownloadRecord>> getDownloadRecord() {
        return mRxDownload.getTotalDownloadRecords();
    }

    @Override
    public RxDownload getRxDownload() {
        return mRxDownload;
    }

    @Override
    public Observable<List<AndroidApk>> getLocalApks() {

        final String dir = ACache.get( mContext ).getAsString( Constant.APK_DOWNLOAD_DIR );
        // TODO: 2018/1/17 just报错
        return Observable.create( new ObservableOnSubscribe<List<AndroidApk>>() {
            @Override
            public void subscribe(ObservableEmitter<List<AndroidApk>> e) throws Exception {

                e.onNext( scanApks( dir ) );
                e.onComplete();
            }
        } );
        //    return Observable.just( scanApks( dir ) );
    }

    @Override
    public Observable<List<AndroidApk>> getInstalledApps() {

        return Observable.create( new ObservableOnSubscribe<List<AndroidApk>>() {
            @Override
            public void subscribe(ObservableEmitter<List<AndroidApk>> e) throws Exception {
                e.onNext( AppUtils.getInstalledApps( mContext ) );
                e.onComplete();
            }
        } );
    }


    private List<AndroidApk> scanApks(String dir) {
        File file = new File( dir );

        if (!file.isDirectory()) {
            throw new RuntimeException( "is not Dir" );
        }
        File[] apks = file.listFiles( new FileFilter() {
            @Override
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return false;
                }
                return f.getName().endsWith( ".apk" );
            }
        } );

        List<AndroidApk> androidApks = new ArrayList<>();

        for (File apk : apks) {
            AndroidApk androidApk = AndroidApk.read( mContext, apk.getPath() );
            if (androidApk != null)
                androidApks.add( androidApk );
        }

        return androidApks;


    }


}
